package com.isoft.service.flight.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.isoft.dao.CommonMetaDao;
import com.isoft.dao.flight.AirportInfoDao;
import com.isoft.entity.CommonMeta;
import com.isoft.entity.flight.AirportInfo;
import com.isoft.service.flight.AirportInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
@Slf4j
@Service
public class AirportInfoServiceImpl extends ServiceImpl<AirportInfoDao, AirportInfo> implements AirportInfoService {
    @Autowired
    private AirportInfoDao airportInfoDao;
    @Autowired
    private CommonMetaDao commonMetaDao;
    @Autowired
    private CommonMeta commonMeta ;
    @Override
    public List<AirportInfo> getAllAirportInfo() {
        return airportInfoDao.getAllAirportInfo();
    }

    @Override
    public List<AirportInfo> getAirportInfoBy(Integer METAID, String ENNM,String CNNM) {
        return airportInfoDao.getAirportInfoBy(METAID, ENNM,CNNM);
    }

    @Transactional // 确保这些操作在一个事务中执行
    public boolean addAirportInfo(AirportInfo airportInfo) {
//            因为是外键关联，所以先要插入meta表
            if ( commonMetaDao.selectAllSEQN(airportInfo.getMETA_ID()) != null) {
                return false;
            }
            log.info("META_ID",commonMetaDao.selectAllSEQN(airportInfo.getMETA_ID()));
            log.info("Received AirportInfo: {}", airportInfo);
            commonMeta.setSEQN(airportInfo.getMETA_ID());
            commonMeta.setCREATETIME(new Date());
            commonMeta.setSNDR("FIMS");
            commonMeta.setDDTM(new Date());
            commonMeta.setTYPE("BASE");
            commonMeta.setSTYP("APUE");
            commonMetaDao.insertCommonMeta(commonMeta);
////            关闭插入自增
//            airportInfoDao.enableIdentityInsert();
//            插入
            int result = airportInfoDao.insert(airportInfo);
            return result > 0;
    }


    @Override
    public boolean deleteAirportInfo(Integer ID) {
        AirportInfo airportInfo = airportInfoDao.selectById(ID);
        int result = airportInfoDao.deleteById(ID);
        if(result > 0){
            commonMetaDao.deleteCommonMeta(airportInfo.getMETA_ID());
        }
        return result > 0;
    }

    /**
     * 分页查询
     */
    public IPage<AirportInfo> getAirportInfoPage(int currentPage, int pageSize) {
        try {
            // 构建分页对象
            Page<AirportInfo> page = new Page<>(currentPage, pageSize);

            LambdaQueryWrapper<AirportInfo> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.select(AirportInfo::getID,AirportInfo::getCODE,AirportInfo::getCNNM,AirportInfo::getENNM,AirportInfo::getAISO,AirportInfo::getAPSN,AirportInfo::getMETAID,AirportInfo::getVALID);
            queryWrapper.orderByDesc(AirportInfo::getID); // 按ID降序排序
            Page<AirportInfo> airportInfoPage = airportInfoDao.selectPage(page,queryWrapper);
            // 封装返回
            return airportInfoPage;
        } catch (Exception e) {
            // 异常处理逻辑，此处可根据需要抛出异常或处理后返回默认值
            throw new RuntimeException("获取数据失败：" + e.getMessage());
        }
    }

    @Override
    public boolean updateAirportInfo(AirportInfo airportInfo) {
        return airportInfoDao.updateById(airportInfo)>0;
    }
}
